<?php namespace Common\Model;
use Hdphp\Model\Model;

class Brand extends Model
{
	protected $tables="Brand";
	protected $validate=array(
		array('bname','required','品牌名称不能为空',3,3),
		 array('bsort','num:0,65535','分类排序必须为数字',3,3),
		);
	protected $auto=array(
		array('logo','image','method',3,3),
		);
	public function image()
	{
		$uploadDir='Upload/'.date('ymd');
    	$file=Upload::type('jpg,png,gif')->path($uploadDir)->size(2000000)->make();
    	if ($file) {
    		return $file[0]['path'];
    	}
    	return '';			
	}
	public function store()
	{
		if(!$this->create()) return false;
    	if ($_FILES['logo']['error']!=4) {
    		if ($error=upload::getError()) {
    			$this->error=$error;
    			return false;
    		}
    	}
    	return $this->add();
	}
	public function edit($bid)
	{
		if(!$this->create()) return false;
		if ($_FILES['logo']['error']!=4) {
			if ($error=upload::getError()) {
				$this->error=$error;
    			return false;
			}
		}
		if(!array_key_exists("hot",$this->data)){
			$this->data['hot']=0;
		}
		$this->where("bid={$bid}")->save();
		return true;
	}
}
 ?>